package com.haircut.api.login;

import com.haircut.sys.constant.Constants;
import com.haircut.sys.service.login.ILoginService;
import com.haircut.sys.service.role.IRoleService;
import com.haircut.sys.utils.menu.MenuUtils;
import com.haircut.sys.utils.user.SecurityUtils;
import com.haircut.sys.vo.menu.MenuReqVo;
import com.haircut.sys.vo.menu.MenuVo;
import com.haircut.sys.vo.menu.RouterVo;
import com.haircut.sys.vo.user.LoginReqVo;
import com.haircut.sys.vo.user.LoginUserVo;
import com.haircut.sys.vo.user.UserVo;
import com.modular.result.RspMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "登录信息")
@RestController
@RequestMapping("/api")
public class ApiLoginController {

    @Autowired
    private ILoginService loginService;
    @Autowired
    private IRoleService roleService;

    @ApiOperation(value = "登录")
    @PostMapping("/login")
    public RspMessage<LoginUserVo> login(@RequestBody LoginReqVo loginVo){
        loginVo.setHasAuthorities(Boolean.FALSE);
       return RspMessage.success(loginService.login(loginVo));
    }

    @ApiOperation(value = "登录")
    @GetMapping("/getInfo")
    public RspMessage<LoginUserVo> getInfo(){
        return RspMessage.success(loginService.getInfo());
    }
    @ApiOperation(value = "登录")
    @PostMapping("/logout")
    public RspMessage<Void> logout(){
        loginService.logout();
        return RspMessage.success();
    }
    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public RspMessage<List<RouterVo>> getRouters()
    {
        LoginUserVo userVo = SecurityUtils.getLoginUser();
        MenuReqVo reqVo = new MenuReqVo();
        reqVo.setRoleId(userVo.getRoleId());
        reqVo.setParentId(Constants.DEFAULT_PARENT_ID);
        List<MenuVo> menuVoList = roleService.findMenuByParentId(reqVo);

        return RspMessage.success(MenuUtils.buildMenus(menuVoList));
    }

    /**
     * 修改用户状态
     * @param
     * @return
     */
    @ApiOperation(value = "查询当前登录的用户信息")
    @GetMapping("profile")
    public RspMessage<UserVo> getUserProfile(){
        return RspMessage.success(loginService.getUserProfile());
    }



}
